* programs
program drop _all


***************************
program define simulate_trajectories

/* 
Program takes choice probabilities and beliefs from the matlab estimates and 
converts them into the proportion of girls who are married and in school at each age. 
It does so separately for each combination of whether or not a girl likes school 
and whether or not she complies with gender norms.
*/

syntax, [counter(name) bs(name)]

foreach like in 0 1 {
	foreach good in 0 1 {
		import excel "$bs_choiceprobs/`counter'choiceprobs_like`like'_good`good'`bs'.xlsx", sheet("Sheet1") firstrow clear

		tempfile choiceprobs
		save `choiceprobs'
		
		import excel "$bs_choiceprobs/`counter'beliefs_good`good'`bs'.xlsx", sheet("Sheet1") firstrow clear

		merge 1:1 age ed inschool using `choiceprobs'
				
		gen goodgirl=`good'
		gen likeschool=`like'

		gen PrSchoolStart=.
		gen PrSchoolEnd=.

		replace PrSchoolStart=1 if age==13 & inschool==1 

		foreach i of numlist 13/19 {
			replace PrSchoolEnd= PrSchoolStart* ( ///
				Pr_HighOffer*PrSch_high ///
				+ Pr_LowOffer*PrSch_low ///
				+ (1-Pr_HighOffer-Pr_LowOffer)*PrSch_nooffer	)
			sum PrSchoolEnd if age==`i'
			replace PrSchoolStart=r(mean) if age==`i'+1 & inschool==1
			}
		gen pr_out=1-PrSchoolStart	
		
		expand 2 if inschool==., gen(married)
		sort age inschool married ed

		gen PrMarry=Pr_LowOffer*PrMarry_low+Pr_HighOffer*PrMarry_high if married==0
		gen PrSchool=Pr_LowOffer*PrSch_low+Pr_HighOffer*PrSch_high+(1-Pr_HighOffer-Pr_LowOffer)*PrSch_nooffer


		gen PrState=0
		order age ed inschool current_sch married PrState

		replace PrState=PrSchoolStart if inschool==1 & age==13
		replace PrState=0 if married==1 & age==13
		replace PrState=0  if age==13 & inschool~=1 & married==0 

		* loop through ages filling in state probabilities
		foreach a of numlist 13/21 {
			
			* 1) following inschool==1
			sum current_sch  if age==`a' & inschool==1
			local current_sch=r(mean)
			sum PrSchool if age==`a' & inschool==1
			local pschool=r(mean)
			sum PrMarry if age==`a' & inschool==1
			local pmarry=r(mean)
			sum PrState if age==`a' & inschool==1
			local pstate=r(mean)
			
			di "`a' -- `current_sch' - `pmarry' - `pschool' - `pstate'"

			replace PrState=`pstate'*`pschool' if age==`a'+1  & ed==`current_sch' & inschool==1 & married==0
			replace PrState=`pstate'*(1-`pschool'-`pmarry') if age==`a'+1 & inschool==. & ed==`current_sch' & married==0
			replace PrState=`pstate'*`pmarry' if age==`a'+1 & inschool==. & ed==`current_sch' & married==1
				
			if `a'==18 {
					replace PrState=PrState+`pstate'*`pschool' if age==`a'+1  & ed==`current_sch' & inschool==. & married==0
				}	
			
			
			* 2) following inschool==0 & married==0, loop through education levels
			levelsof ed if age==`a', local(levels)
			
			foreach e in `levels' {
				di "a=`a', e=`e'"
				sum PrState if age==`a' & inschool==. & ed==`e' & married==0
				local pstate=r(mean)
				sum PrMarry if age==`a' & inschool==. & ed==`e' & married==0
				local pmarry=r(mean)
				* add on probability already married
				sum PrState if age==`a' & inschool==. & ed==`e' & married==1
				local p_alreadymarried=r(mean)

				replace PrState=`pstate'*`pmarry'+`p_alreadymarried' if age==`a'+1 & inschool==. & ed==`e' & married==1
				replace PrState=PrState+`pstate'*(1-`pmarry') if age==`a'+1 & inschool==. & ed==`e' & married==0
				}
				
			total PrState if age==`a'+1	
			}

		total PrState , over(age)

		tempfile temp`like'`good'
		save `temp`like'`good'', replace
	}
}



use `temp00'
append using `temp01'
append using `temp10'
append using `temp11'

replace PrState=0 if PrState<0

gen counter="`counter'"
end


* collapse trajectories
************************
program define collapse_trajectories

/*
Program collapses implied trajectories both unconditionally and conditionally on a daughter liking school (or not) and complying with norms (or not)
*/

syntax, [by(string)]

* create goodgirl-likeschool weight based on observed sample proportions (calculated in do file 3a)
/*
parents_wo |     hard_working
     rried |         0          1 |     Total
-----------+----------------------+----------
         0 |     1,802      1,157 |     2,959 
         1 |     1,345        372 |     1,717 
-----------+----------------------+----------
     Total |     3,147      1,529 |     4,676 
*/

gen weight=0
replace weight=1802/4676 if likeschool==0 & goodgirl==1
replace weight=1157/4676 if likeschool==1 & goodgirl==1
replace weight=1345/4676 if likeschool==0 & goodgirl==0
replace weight=372/4676 if likeschool==1 & goodgirl==0

gen PrH_givenS=1-PrSchool-PrMarry if inschool==1
gen PrS_givenS=PrSchool if inschool==1
gen PrM_givenS=PrMarry if inschool==1


* collapse to age
recode inschool (.=0)

gen inschool_end=PrSchool
replace inschool_end=0 if inschool==0

gen mar_end=PrMarry
replace mar_end=1 if married==1

collapse married inschool inschool_end  mar_end weight Pr*givenS [pw=PrState], by(age likeschool goodgirl)
replace inschool=. if inschool==0
gen unmarried_end=1-mar_end
gen one=1

* unconditional average trajectories
rename married married_st
rename inschool inschool_st

if "`by'"=="likeschool" {
	collapse married_st inschool_st  inschool_end mar_end [pw=weight], by(age likeschool)
	di "hello"
	
}
if "`by'"== "goodgirl"  {
	collapse married_st inschool_st  inschool_end mar_end [pw=weight], by(age goodgirl)
	
}
if "`by'"~= "goodgirl" &  "`by'"~= "likeschool"  &  "`by'"~= "goodgirl,likeschool" {
	collapse married_st inschool_st  inschool_end mar_end [pw=weight], by(age)
}
if "`by'"== "goodgirl,likeschool"  {	
}
cap gen unmarried_end=1-mar_end
cap gen one=1

end
